/**
 * Copyright (c) 2011 Paul Wagner
 */
package com.pew.tasktimer.store;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;

import javax.annotation.Resource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.*;

import com.pew.tasktimer.domain.Task;
import com.pew.tasktimer.domain.TaskTimerPackage;

/**
 * @author Elwin
 * Created: Mar 29, 2011
 * Purpose: 
 *
 */
public class JAXBStore {
	
	static Logger cLogger = Logger.getLogger(JAXBStore.class);
		
	public static void storeXml(Object myObjectToStore, String myFilePath) {
		try {
			JAXBContext context = JAXBContext.newInstance(myObjectToStore.getClass());
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			
			Writer w = null;
			try {
				w = new FileWriter(myFilePath);
				m.marshal(myObjectToStore, w);
			} finally {
				try {
					w.close();
				} catch (Exception ex) {
					cLogger.error("Could not write to file.", ex);
				}
			}
			
		} catch(Exception ex)
		{
			cLogger.error("Could not create XML.", ex);
		}
	}
	
	
	public static TaskTimerPackage readXmlPackage(String myFilePath){
		TaskTimerPackage lTTP = null;
		FileReader lFileReader = null;
		try {
			JAXBContext context = JAXBContext.newInstance(TaskTimerPackage.class);
			lFileReader = new FileReader(myFilePath);
			Unmarshaller um = context.createUnmarshaller();
			lTTP = (TaskTimerPackage) um.unmarshal(lFileReader);
			
		
		} catch (Exception ex)
		{
			cLogger.error("Couldn't unmarshall", ex);
		}
		finally {
			if(lFileReader != null) {
				try {
					lFileReader.close();
				} catch (Exception ex)
				{
					cLogger.warn("Tried to close file but couldn't", ex);
				}
			}
		}
		
		return lTTP;
	}
	
	public static void transformAndStore(Object myObjectToStore, String myXslt, String myFilePath) {
		
		Writer w = null;
		try {
			JAXBContext context = JAXBContext.newInstance(myObjectToStore.getClass());
			JAXBSource source = new JAXBSource( context, myObjectToStore );
			w = new FileWriter(myFilePath);
	       
	       // set up XSLT transformation
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer t = tf.newTransformer(new StreamSource(ClassLoader.getSystemResourceAsStream(myXslt)));
			t.transform(source,new StreamResult(w));
			w.close();
		} catch (Exception ex) {
			cLogger.error("Could not create transformation with " + myXslt, ex);
		}

		
	}
	
	
	
	
}
